﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Packet.InPacket
{
    /// <summary>
    ///  *   更新用户挂起信息包
    ///  *   基本包数据结构
    ///  *   用户名 
    ///  *   用户更新数据库关联字段
    ///  *   用户更新新值
    /// </summary>
    class UpdateUserInfoPacket : BaseInPackage
    {
        public UpdateUserInfoPacket(InPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //读取用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //关联字段长度
            byte[] DbFiledLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, DbFiledLength, 0, 4);
            Offset += 4;
            //关联字段
            byte[] DbFiled = new byte[BitConverter.ToInt32(DbFiledLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, DbFiled, 0, DbFiled.Length);
            Offset += DbFiled.Length;
            //新值长度
            byte[] NewValueLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, NewValueLength, 0, 4);
            Offset += 4;
            //新值
            byte[] NewValue = new byte[BitConverter.ToInt32(NewValueLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, NewValue, 0, NewValue.Length);
            Offset += NewValue.Length + 2;
            //IP地址长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP地址
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //聊天端口
            byte[] Talkport = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Talkport, 0, 4);


            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("DBFILED", DbFiled);
            this.DIC_CONTENT.Add("VALUE", NewValue);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
            this.DIC_CONTENT.Add("TALKPORT", Talkport);
        }

        /// <summary>
        ///     得到当前更新的数据库关联字段
        /// </summary>
        /// <returns>
        ///     返回数据库关联字段
        /// </returns>
        public String GetDbFiled()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["DBFILED"]);
        }

        /// <summary>
        ///     得到当前更新的新值
        /// </summary>
        /// <returns>
        ///     返回更新的新值
        /// </returns>
        public String GetNewValue()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["VALUE"]);
        }
    }
}
